﻿// My methods here.
//-----------------------------------------------------------------------
// <Copyright file=GroupNews company=DHPD>
// Copyright (c) DHPD Corporation (http://dhpd.edu.vn). All rights reserved.
// </copyright>
// <author>HP</author>
//-----------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KM.DAT;
using KM.ENT;
using KM.Models;

namespace KM.DAT
{
    /// <summary>
    /// This is datGroupNews class
    /// </summary>

    public class datGroupNews
    {
        public datGroupNews()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        SqlCommand cmd;
        SqlDataReader dr;
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        #region  === tạo mới và sửa GroupNews===
        public void createEdit(entGroupNews entGN)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_GroupNewsCreateEdit", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[12];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[1] = new SqlParameter("Name", DbType.Int32);
            sqla[2] = new SqlParameter("Tag", DbType.Int32);
            sqla[3] = new SqlParameter("Title", DbType.Int32);
            sqla[4] = new SqlParameter("Description", DbType.Int32);
            sqla[5] = new SqlParameter("Keyword", DbType.Int32);
            sqla[6] = new SqlParameter("Ord", DbType.Int32);
            sqla[7] = new SqlParameter("Priority", DbType.Int32);
            sqla[8] = new SqlParameter("Index", DbType.Int32);
            sqla[9] = new SqlParameter("Active", DbType.Int32);
            sqla[10] = new SqlParameter("Lang", DbType.Int32);
            sqla[11] = new SqlParameter("ParentId", DbType.Int32);

            sqla[0].Value = entGN.Id;
            sqla[1].Value = entGN.Name;
            sqla[2].Value = entGN.Tag;
            sqla[3].Value = entGN.Title;
            sqla[4].Value = entGN.Description;
            sqla[5].Value = entGN.Keyword;
            sqla[6].Value = entGN.Ord;
            sqla[7].Value = entGN.Priority;
            sqla[8].Value = entGN.Index;
            sqla[9].Value = entGN.Active;
            sqla[10].Value = entGN.Lang;
            sqla[11].Value = entGN.ParentId;


            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion

        #region  === getall GroupNews===
        public List<entGroupNews> getAll()
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_GroupNewsGetAll", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;

            dr = cmd.ExecuteReader();
            entGroupNews entGN = new entGroupNews();
            List<entGroupNews> lst = new List<entGroupNews>();
            while (dr.Read())
            {
                entGN = new entGroupNews();
                entGN.Id = Convert.ToInt32(dr["Id"].ToString());
                entGN.Name = dr["Name"].ToString();
                entGN.Tag = dr["Tag"].ToString();
                entGN.Title = dr["Title"].ToString();
                entGN.Description = dr["Description"].ToString();
                entGN.Keyword = dr["Keyword"].ToString();
                //entGN.Ord = Convert.ToInt32(dr["Ord"].ToString());
                //entGN.Priority = Convert.ToInt32(dr["Priority"].ToString());
                entGN.Index = Convert.ToInt32(dr["Index"].ToString());
                //entGN.Active = Convert.ToInt32(dr["Active"].ToString());
                //entGN.Lang = dr["Lang"].ToString();
                entGN.ParentId = Convert.ToInt32(dr["ParentId"].ToString());

                lst.Add(entGN);
            }
            dr.Close();
            sqlConnection.Close();
            return lst;
        }
        #endregion
        #region  === getall GroupNews by parentid===
        public List<entGroupNews> getAllByParentId(int Id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_GroupNewsGetAllByParentId", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ParentId", Id);
            dr = cmd.ExecuteReader();
            entGroupNews entGN = new entGroupNews();
            List<entGroupNews> lst = new List<entGroupNews>();
            while (dr.Read())
            {
                entGN = new entGroupNews();
                entGN.Id = Convert.ToInt32(dr["Id"].ToString());
                entGN.Name = dr["Name"].ToString();
                entGN.Tag = dr["Tag"].ToString();
                entGN.Title = dr["Title"].ToString();
                entGN.Description = dr["Description"].ToString();
                entGN.Keyword = dr["Keyword"].ToString();
                //entGN.Ord = Convert.ToInt32(dr["Ord"].ToString());
                //entGN.Priority = Convert.ToInt32(dr["Priority"].ToString());
                entGN.Index = Convert.ToInt32(dr["Index"].ToString());
                //entGN.Active = Convert.ToInt32(dr["Active"].ToString());
                //entGN.Lang = dr["Lang"].ToString();
                entGN.ParentId = Convert.ToInt32(dr["ParentId"].ToString());
                entGN.Url = dr["Url"].ToString();

                lst.Add(entGN);
            }
            dr.Close();
            sqlConnection.Close();
            return lst;
        }
        #endregion

        #region  === getinfo GroupNews===
        public entGroupNews getInfo(int Id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_GroupNewsGetInfo", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = Id;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entGroupNews entGN = new entGroupNews();
            if (dr.Read())
            {
                entGN.Id = Convert.ToInt32(dr["Id"].ToString());
                entGN.Name = dr["Name"].ToString();
                entGN.Tag = dr["Tag"].ToString();
                entGN.Title = dr["Title"].ToString();
                entGN.Description = dr["Description"].ToString();
                entGN.Keyword = dr["Keyword"].ToString();
                entGN.Ord = Convert.ToInt32(dr["Ord"].ToString());
                entGN.Priority = Convert.ToInt32(dr["Priority"].ToString());
                entGN.Index = Convert.ToInt32(dr["Index"].ToString());
                entGN.Active = Convert.ToInt32(dr["Active"].ToString());
                entGN.Lang = dr["Lang"].ToString();
                entGN.ParentId = Convert.ToInt32(dr["ParentId"].ToString());

            }
            dr.Close();
            sqlConnection.Close();
            return entGN;
        }
        #endregion

        #region === xoa GroupNews===
        public void delete(int id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_GroupNewsDelete", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = id;

            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion
    }
}
